import { createSlice,createAsyncThunk } from '@reduxjs/toolkit';
import { findById } from '../../services/coupon';

// 创建中间件函数，其中回调函数返回的数据会交给reducer
export const fetchCoupon = createAsyncThunk('coupon/fetchCoupon',async id => {
    let coupon = await findById(id);
    return coupon;
});

const couponSlice = createSlice({
    name:"coupon",
    initialState:{
        updateVisible:false,
        coupon:{}
    },
    reducers:{
        setUpdateVisible(state,action){
            state.updateVisible = action.payload;
        },
       
    },
    extraReducers(build){
        build.addCase(fetchCoupon.fulfilled,(state,action) => {
            state.coupon = action.payload;
        });
    }
});
export const {setUpdateVisible} = couponSlice.actions;
export default couponSlice.reducer;